<?php

/**
 * This file is part of Switcher.
 * 
 * Switcher is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Switcher is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Switcher. If not, see <http://www.gnu.org/licenses/>.
 * 
 * @license http://www.opensource.org/licenses/gpl-3.0.html GPL-3.0
 */

namespace mustache;

use switcher\utils\FileHelper;

final class SpecTest extends \PHPUnit_Framework_TestCase implements PartialLoader
{
	private $currentTest;
	
	/** @dataProvider specs */
	public function testShouldComplyWithSpecs($test)
	{
		$this->currentTest = $test;
		
		$parser = new Parser($test->template, $test->data, $this);
		$this->assertEquals($test->expected, $parser->render(), "$test->name : $test->desc");
	}
	
	public function loadPartial($partial)
	{
		return $this->currentTest->partials->$partial;
	}
	
	public function specs()
	{
		$directory = dir($this->baseDirectory() . '/specs');
		
		$tests = array();
		
		while (($item = $directory->read()) != false)
		{
			if ( fnmatch('*.json', $item) )
			{
				$spec = json_decode( FileHelper::loadFile($directory->path.'/'.$item) );
				
				foreach ($spec->tests as $test)
				{
					array_push($tests, array($test));
				}
			}
		}
		
		return $tests;
	}
	
	private function baseDirectory()
	{
		$fileInfo = pathinfo(__FILE__);
		return $fileInfo['dirname'];
	}
}

?>